//
//  GXZTaglistView.m
//  GXZTagListView
//
//  Created by Deeokay on 2017/10/27.
//  Copyright © 2017年 GXZ. All rights reserved.
//

#import "GXZTaglistView.h"
@interface GXZTaglistView ()
@property (nonatomic,assign) CGFloat tagHeight;
@end
@implementation GXZTaglistView
-(instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}
-(instancetype)gxzAlocWihtInit:(CGRect)frame andTagList:(NSMutableArray *)tagList{
    if (self == [super initWithFrame:frame]) {
        [self gxzCountTagWithFrame:frame andTaglist:tagList];
    }
    return self;
}
-(void)gxzCountTagWithFrame:(CGRect)frame andTaglist:(NSMutableArray *)taglist{
    NSLog(@"在这里可以做自己的操作");
}
-(void)gxzSetUIandTaglist:(NSMutableArray *)taglist withHeight:(void (^)(CGFloat))transformHeight{
    CGFloat x = _tagX ? _tagX : 20;
    CGFloat y = _tagY ? _tagY : 20;
    CGFloat marginX = _tagXMargin ? _tagXMargin : 10;
    CGFloat marginY = _tagYMargin ? _tagYMargin : 10;
    CGFloat totalWidth = self.frame.size.width;
    UIButton * lastBtn;
    CGFloat row = 0;
   
    for (int i = 0; i < taglist.count; i++) {
        UIButton * tagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       
        tagBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [tagBtn addTarget:self action:@selector(gxzTapTag:) forControlEvents:UIControlEventTouchUpInside];
        [tagBtn setTitleColor:_defaultTitleColor ? _defaultTitleColor : [UIColor lightGrayColor] forState:UIControlStateNormal];
        [tagBtn setTitleColor:_defaultTitleSelectColor ? _defaultTitleSelectColor : [UIColor blueColor] forState:UIControlStateSelected];
        tagBtn.tag = i;
        tagBtn.layer.cornerRadius = _defaultCornerRadius ? _defaultCornerRadius : 5;
        tagBtn.layer.borderColor = [_defaultBorderColor ? _defaultBorderColor : [UIColor lightGrayColor] CGColor];

        tagBtn.layer.borderWidth = _defaultBorderWidth ? _defaultBorderWidth : 1;
        tagBtn.layer.masksToBounds = YES;
        [tagBtn setTitle:[NSString stringWithFormat:@"%@",taglist[i]] forState:UIControlStateNormal];
        [tagBtn sizeToFit];
        if (i == 0) {
            x = _tagX ? _tagX : 20;
        }else{
            x = x + lastBtn.frame.size.width +  marginX;
        }
        if (row == 0) {
            y = _tagY ? _tagY : 20;
        }
        if (x + lastBtn.frame.size.width + (_tagX ? _tagX : 20) > totalWidth) {
            x = _tagX ? _tagX : 20;
            row += 1;
            y = _tagY ? _tagY : 20 + lastBtn.frame.size.height * row + row * marginY;
        }
        tagBtn.frame = CGRectMake(x, y, tagBtn.frame.size.width + (_defaultTagWidth ? _defaultTagWidth : 10), tagBtn.frame.size.height + (_defaultTagHeight ? _defaultTagHeight : 5));
       
        [self addSubview:tagBtn];
        lastBtn = tagBtn;
    }
    _tagHeight = y + lastBtn.frame.size.height + (_tagY ? _tagY : 20);
    [self gxzTransformHeight:transformHeight];
}
-(CGFloat)gxzGiveYouHeightOfView{
    return _tagHeight;
}
-(void)gxzTransformHeight:(void (^)(CGFloat))transformHeight{
    transformHeight(_tagHeight);
}
-(void)gxzTapTag:(UIButton *)sender{
    [self.delegate gxzTagListView:self didSelectedAtIndex:sender.tag showTitle:sender.titleLabel.text];
}
@end
